import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import VueBus from './vue-bus'

Vue.use(VueBus)
Vue.config.productionTip = false

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')

//创建自定义指令
Vue.directive('focus', {
  //指令选项
  bind() {
    console.log('只会调用一次')
  },
  inserted(el, binding) {
    console.log('被绑定元素插入父节点时调用')
    el.focus()
    console.log(binding)
  },
  update() {
    console.log('被绑定元素所在模板更新时调用')
  },
  componentUpdated() {
    console.log('被绑定元素所在模板完成一次更新周期时调用')
  },
  unbind() {
    console.log('只调用一次,指令与元素解绑时调用')
  }
})

//自定义指令 外部点击关闭下拉菜单
Vue.directive('clickOutside', {
  bind(el, binding, vnode) {
    function documentHandle(e) {
      if(el.contains(e.target)) {
        return false
      }
      if(binding.expression) {
        binding.value(e)
      }
    }
    el._vueClickOutside_ = documentHandle
    document.addEventListener('click', documentHandle)
  },
  unbind(el, binding) {
    document.removeEventListener('click', el._vueClickOutside_)
    delete el._vueClickOutside_
  }
})

//自定义指令  时间格式转换
Vue.directive('time', {
  bind(el, binding) {
    el.innerHTML = time.getFormatTime(binding.value)
    el._timeout_ = setInterval(() => {
      el.innerHTML = time.getFormatTime(binding.value)
    }, 60000);
  },
  unbind(el) {
    clearInterval(el._timeout_)
    delete el._timeout_
  }
})

//时间转换函数
let time = {
  //获取当前时间戳
  getUnix() {
    let date = new Date()
    return date.getTime()
  },
  //获取今天0点0分的时间戳
  getTodayUnix() {
    let date = new Date()
    date.setHours(0)
    date.setMinutes(0)
    date.setSeconds(0)
    date.setMilliseconds(0)
    return date.getTime()
  },
  //获取今年1月1日0点0分0秒的时间戳
  getYearUnix() {
    let date = new Date()
    date.setMonth(0)
    date.setDate(1)
    date.setHours(0)
    date.setMinutes(0)
    date.setSeconds(0)
    date.setMilliseconds(0)
    return date.getTime()
  },
  //获取标准年月日
  getLastData(time) {
    let date = new Date(time)
    let month = date.getMonth() + 1 < 10 ? '0' + ( date.getMonth() + 1 ) : date.getMonth() + 1
    let day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
    return date.getFullYear() + '-' + month + '-' + day
  },
  //转换时间
  getFormatTime(timestamp) {
    let now = this.getUnix()  //当前时间戳
    let today = this.getTodayUnix() //今天0点时间戳
    let year = this.getYearUnix()   //今年0点时间戳
    let timer = (now - timestamp) / 1000  //转换为秒级时间戳
    let tip = ''
    if(timer <= 0) {
      tip = '刚刚'
    }else if(Math.floor(timer/60) <= 0) {
      tip = '刚刚'
    }else if(timer < 60*60) {
      tip = Math.floor(timer/60) + '分钟前'
    }else if(timer >= 60*60 && timestamp - today >= 0) {
      tip = Math.floor(timer/(60*60)) + '小时前'
    }else if(timer/(60*60*24) <= 31) {
      tip = Math.ceil(timer/(60*60*24)) + '天前'
    }else {
      tip = this.getLastData(timestamp)
    }
    return tip
  }
}